package com.example.listossimage.util;

import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created by Chenzq on 2018/3/1.
 */
public class TimeUtil {
    public final static String format = "yyyy-MM-dd HH:mm:ss";
    public final static String formatChinese = "yyyy年MM月dd日HH时mm分";
    public final static String formatMillis = "yyyy-MM-dd HH:mm:ss.SSS";
    public final static String formatWithSlash = "yyyy/MM/dd HH:mm:ss";
    public final static String formatWithSlashMin = "yyyyMMddHHmmss";
    public final static String formatYmdWithNoSlash = "yyyyMMdd";

    public final static String formatYMDNoSlash = "yyyyMMdd";
    public final static String formatWithSlashLong = "yyyy/MM/dd HH:mm:ss.SSS";
    public final static String formatWithSlash2 = "yyyy/MM/dd";
    public final static String formatWithSlash3 = "yyyy/MM";
    public final static String formatWithHM = "yyyy-MM-dd HH:mm";
    public final static String formatWithYMD = "yyyy-MM-dd";
    public final static String formatWithHMS = "HH:mm:ss";
    public final static String format2 = "yyyy-MM-dd'T'HH:mm:ss";
    public final static String defaultTimeZone = "+08:00";
    private final static String EARLIEST_TIME = "0001/01/01 00:00:00";
    private final static String LASTEST_TIME = "9999/12/31 23:59:59";

    public static Date toDayStartPoint(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 0, 0);
        return c.getTime();
    }

    public static Date toDayEndPoint(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        return c.getTime();
    }

    public static String toString(Date time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(time);
    }

    public static String toString(Date time, String format) {

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(time);
    }

    public static Date getNow() {

        Calendar calendar = Calendar.getInstance();
        return calendar.getTime();
    }

    /**
     * 计算当前日期是周几
     *
     * @param calendar
     * @return
     */
    public static int getDayOfWeek(Calendar calendar) {
        //一周第一天是否为星期天
        boolean isFirstSunday = (calendar.getFirstDayOfWeek() == Calendar.SUNDAY);
        //获取周几
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
        //若一周第一天为星期天，则-1
        if (isFirstSunday) {
            weekDay = weekDay - 1;
            if (weekDay == 0) {
                weekDay = 7;
            }
        }

        return weekDay;
    }

    /**
     * 计算当前日期是周几
     *
     * @param time
     * @return
     */
    public static String getDayOfWeek(Date time) {
        String[] weekDays = {"SUNDAY","MONDAY","TUESDAY","WEDNESDAY","THURSDAY","FRIDAY","STAURDAY"};
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        //获取周几
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
        return weekDays[weekDay-1];
    }

    public static Date getDateByString(String str, String formatStr) throws ParseException {
        if (str == null) {
            return null;
        }
        try {
            return getDateByStringAct(str, formatStr);
        } catch (ParseException e) {
            try {
                return getDateByStringAct(str, format);
            } catch (ParseException e1) {
                return getDateByStringAct(str, formatWithSlash);
            }
        }
    }

    public static Date getDateByStringAct(String str, String formatStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        Date date = null;
        // 关闭宽松转换，当格式不对时直接抛出异常而不是转成错误时间
        format.setLenient(false);
        date = format.parse(str);
        return date;
    }

    public static Date addSecond(Date date, int second) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.SECOND, second);
        date = c.getTime();
        return date;
    }

    public static String getNowString() {
        return getStringByDate(getNow(), format);
    }

    public static String getNowLongString() {
        return getStringByDate(getNow(), formatWithSlashLong);
    }

    public static String getNowString(String formatStr) {
        return getStringByDate(getNow(), formatStr);
    }

    public static String getStringByDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    public static Date getBeginTimeOfDay() throws Exception {
        String str = getStringByDate(new Date(), "yyyy-MM-dd");
        return getDateByString(str + " 00:00:00", format);
    }

    public static Date getBeginTimeOfDay(Date now) throws Exception {
        String str = getStringByDate(now, "yyyy-MM-dd");
        return getDateByString(str + " 00:00:00", format);
    }

    public static Date getBeginTimeOfDayNoExc(Date now) {
        String str = getStringByDate(now, "yyyy-MM-dd");
        try {
            return getDateByString(str + " 00:00:00", format);
        } catch (ParseException ignored) { }
        return null;
    }

    public static Date getEndTimeOfDay(Date now) throws Exception {
        String str = getStringByDate(now, "yyyy-MM-dd");
        return getDateByString(str + " 23:59:59", format);
    }

    public static Date getEndTimeOfDayNoExc(Date now) {
        String str = getStringByDate(now, "yyyy-MM-dd");
        try {
            return getDateByString(str + " 23:59:59", format);
        } catch (ParseException ignored) { }
        return null;
    }

    public static Date getBeginTimeOfString(String time, String format) throws Exception {
        return getDateByString(time + " 00:00:00", format);
    }

    public static Date getEndTimeOfString(String time, String format) throws Exception {
        return getDateByString(time + " 23:59:59", format);
    }

    public static Date getTimeOfString(String time, String hms, String format) {
        try {
            return getDateByString(time.concat(" ").concat(hms), format);
        } catch (ParseException e) {
            return null;
        }
    }

    public static String gethHms(Date date) {
        return getStringByDate(date, formatWithHM).split(" ")[1];
    }

    public static Date addMonth(Date dateFrom, int i) {
        Calendar date = Calendar.getInstance();
        date.setTime(dateFrom);
        date.add(Calendar.MONTH, i);
        return date.getTime();
    }

    public static Date addDay(Date dateFrom, int i) {
        Calendar date = Calendar.getInstance();
        date.setTime(dateFrom);
        date.add(Calendar.DAY_OF_MONTH, i);
        return date.getTime();
    }

    public static Date addHour(Date date, int hours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY, hours);
        return cal.getTime();
    }

    public static Date addMinute(Date date, int minute) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);
        return cal.getTime();
    }

    /**
     * 获取当前日期前N天的日期
     *
     * @param now
     * @param n
     * @return
     */
    public static Date getDateBefore(Date now, int n) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - n);
        return calendar.getTime();
    }
    /**
     * 获取前N分钟的时间
     * @param time
     * @param minute
     * @return
     */
    public static Date getDateBeforeMinute(Date time, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(Calendar.MINUTE, 0 - minute);
        return calendar.getTime();
    }
    /**
     * @param now
     * @return Date    返回类型
     * @throws
     * @Title: trimHMSToZero
     * @Description: 将一个时间的时分秒归零
     */
    public static Date trimHMSToZero(Date now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(calendar.MINUTE, 0);
        calendar.set(calendar.SECOND, 0);
        calendar.set(calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取当月第一天和最后一天
     *
     * @return
     */
    public static Map<String, Date> getCurMonthFirstdayAndLastday() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date firstday, lastday;
        // 获取前月的第一天
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = cale.getTime();
        // 获取前月的最后一天
        cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastday = cale.getTime();
        Map<String, Date> m = new HashMap<>();
        m.put("mf", firstday);
        m.put("ml", lastday);
        return m;
    }

    /**
     * 获取指定时间月第一天和最后一天
     *
     * @return
     */
    public static Map<String, Date> getCurMonthFirstdayAndLastday(Calendar calendar) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date firstday, lastday;
        // 获取前月的第一天
        Calendar cale = calendar;
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        firstday = cale.getTime();
        // 获取前月的最后一天
        cale = calendar;
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        lastday = cale.getTime();
        Map<String, Date> m = new HashMap<>();
        m.put("mf", firstday);
        m.put("ml", lastday);
        return m;
    }

    /**
     * /计算时间差（秒)
     *
     * @param st
     * @param et
     * @return
     */
    public static long getSecBetweenTime(Date st, Date et) {
        if(st==null || et==null)
            return 0;
        long sec;
        long time1 = st.getTime();
        long time2 = et.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        sec = diff / 1000;
        return sec;
    }

    /**
     * /计算时间差（分)
     *
     * @param st
     * @param et
     * @return
     */
    public static long getMinBetweenTime(Date st, Date et) {
        long sec = getSecBetweenTime(st, et);
        long mins = sec/60;
        if (mins%60 > 0) {
            mins = (mins + 1);
        }
        return mins;
    }

    /**
     * /计算时间差（天)
     *
     * @param st
     * @param et
     * @return
     */
    public static long getDayBetweenTime(Date st, Date et) {
        if(st==null || et==null)
            return 0;
        long day;
        long time1 = st.getTime();
        long time2 = et.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        day = diff / 1000 / 60 / 60 / 24;
        return day;
    }
    /**
     * 获取到期时间
     *
     * @param st
     * @param ttl
     * @return
     */
    public static Date getTtlStr(Date st, long ttl) {
        if (st == null)
            return null;
        return new Date(st.getTime() + ttl * 1000);
    }

    /**
     * 默认最早时间
     *
     * @return
     * @throws ParseException
     */
    public static Date getEarliestTime() throws ParseException {
        return getDateByString(EARLIEST_TIME, formatWithSlash);
    }

    /**
     * 默认最晚时间
     *
     * @return
     * @throws ParseException
     */
    public static Date getLastestTime() throws ParseException {
        return getDateByString(LASTEST_TIME, formatWithSlash);
    }

    public static int getFullDay(Date date) {
        return Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(date));
    }

    public static int getFullMonth(Date date) {
        return Integer.parseInt(new SimpleDateFormat("yyyyMM").format(date));
    }

    public static int getFullYear(Date date) {
        return Integer.parseInt(new SimpleDateFormat("yyyy").format(date));
    }

    public static void main(String[] args) throws Exception {
//        System.out.println(toString(toDayEndPoint(getDateByString("2019-09-20 11:23:45", format))));

//        Date time = Calendar.getInstance().getTime();
//        Date now = TimeUtil.addMonth(time, 2);
//        System.err.println(now);
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(now);
//
//        Map<String, Date> monthFirstTimeAndLastTime = getMonthFirstTimeAndLastTime(calendar);
//        System.err.println("本月最早时间："+monthFirstTimeAndLastTime.get("mf"));
//        System.err.println("本月最晚时间："+monthFirstTimeAndLastTime.get("ml"));

//        Map<String, Date> weekFirstTimeAndLastTime = getWeekFirstTimeAndLastTime(calendar);
//        System.err.println("本周最早时间："+weekFirstTimeAndLastTime.get("wf"));
//        System.err.println("本周最早时间："+weekFirstTimeAndLastTime.get("wl"));
    }

    /**
     * 获取指定时间月最早的时间和最晚的时间（最晚的时间暂定为下月一的零点）
     * @throws Exception
     */
    public static Map<String, Date> getMonthFirstTimeAndLastTime(Calendar calendar) throws Exception {
        Map<String, Date> curMonthFirstdayAndLastday = getCurMonthFirstdayAndLastday(calendar);
        Date mf = getBeginTimeOfDay(curMonthFirstdayAndLastday.get("mf"));
        Date ml = getBeginTimeOfDay(TimeUtil.addDay(curMonthFirstdayAndLastday.get("ml"),1));
        Map<String, Date> m = new HashMap<>();
        m.put("mf", mf);
        m.put("ml", ml);
        return m;
    }

    /**
     * 获取指定时间周最早的时间和最晚的时间（最晚的时间暂定为下周一的零点）
     * @throws Exception
     */
    public static Map<String, Date> getWeekFirstTimeAndLastTime(Calendar calendar) throws Exception {
        int dayOfWeek = getDayOfWeek(calendar);
        Date wf = getBeginTimeOfDay(TimeUtil.addDay(calendar.getTime(), 1 - dayOfWeek));
        Date wl = getBeginTimeOfDay(TimeUtil.addDay(calendar.getTime(), 7 - dayOfWeek + 1));
        Map<String, Date> m = new HashMap<>();
        m.put("wf", wf);
        m.put("wl", wl);
        return m;
    }

    /**
     * 获得n个月前的时间
     *
     * @return
     */
    public static Date getSinceDate(int n) {
        return TimeUtil.addMonth(TimeUtil.getNow(), -1 * n);
    }

    public static TimeRang GetMonthRang(int y, int m) {
        // 获得当前年、月
        Calendar c = Calendar.getInstance();
        c.set(y, m, 1, 0, 0, 0);
        Date st = c.getTime();
        int days = getDays(y, m);
        c.set(y, m, days, 0, 0, 0);
        Date et = c.getTime();
        TimeRang timeRang = new TimeRang(st, et);
        return timeRang;
    }

    /**
     * 一个月有多少天
     *
     * @param year
     * @param month
     * @return
     */
    public static int getDays(int year, int month) {
        int days = 0;
        if (month != 2) {
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    days = 31;
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    days = 30;

            }
        } else {
            // 闰年
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                days = 29;
            else
                days = 28;
        }
        return days;
    }

    /**
     * 为日期设置时间
     * @param src
     * @param hour
     * @param min
     * @return
     */
    public static Date setTime(Date src, int hour, int min) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(src);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, min);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    /**
     * 判断某个值是否在某区间0:在区间内，-1小于区间（未生效），1大于区间(过期)
     * @param catchTime
     * @param beginDay
     * @param endDay
     * @return
     */
    public static int checkTimeBetweenSection(Date catchTime, Date beginDay, Date endDay) {
        if(catchTime.getTime() < beginDay.getTime()){
            return -1;
        }
        if(catchTime.getTime() > endDay.getTime()) {
            return 1;
        }
        return 0;
    }

    /**
     * 判断某个值是否在某区间0:在区间内，-1小于区间（未生效），1大于区间(过期)
     * @param catchTime
     * @param beginDay
     * @param endDay
     * @return
     */
    public static int checkTimeBetweenSection(LocalDateTime catchTime, LocalDateTime beginDay, LocalDateTime endDay) {
        long beginMilliSecond = beginDay == null ? 0 : beginDay.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long endMilliSecond = endDay == null ? Long.MAX_VALUE : endDay.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long catchMilliSecond = catchTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        if(catchMilliSecond < beginMilliSecond){
            return -1;
        }
        if(catchMilliSecond > endMilliSecond) {
            return 1;
        }
        return 0;
    }

    /**
     * 时间范围
     */
    public static class TimeRang {
        public TimeRang(Date st, Date et) {
            this.st = st;
            this.et = et;
        }

        private Date st;
        private Date et;

        public String getStMonthStr() {
            return getString(st);
        }

        public String getEtMonthStr() {
            return getString(et);
        }

        private String getString(Date t) {
            Calendar c = Calendar.getInstance();
            c.setTime(t);
            int y = c.get(Calendar.YEAR);
            int m = c.get(Calendar.MONTH) + 1;
            int d = c.get(Calendar.DAY_OF_MONTH);
            return String.format("%s-%s-%s", y, m, d);
        }
    }

    /**
     * 设置默认时间
     *
     * @param s
     * @param i
     * @return
     * @throws ParseException
     */
    public static Date getDefaultTime(String s, int i) throws ParseException {
        Date d = null;
        switch (i) {
            case 0: {
                d = StringUtils.isBlank(s) ? TimeUtil.getEarliestTime() : TimeUtil.getDateByString(s, TimeUtil.formatWithSlash);
                break;
            }
            case 1: {
                d = StringUtils.isBlank(s) ? TimeUtil.getLastestTime() : TimeUtil.getDateByString(s, TimeUtil.formatWithSlash);
            }
        }
        return d;
    }

    /**
     * /计算时间差（秒)（不满一秒算一秒）
     *
     * @param st
     * @param et
     * @return
     */
    public static int getSecBetweenTimeUpward(Date st, Date et) {
        if (st == null || et == null)
            return 0;
        int sec;
        long time1 = st.getTime();
        long time2 = et.getTime();

        long diff;
        if (time1 < time2) {
            diff = time2 - time1;
        } else {
            diff = time1 - time2;
        }
        double time3 = (double) diff / 1000;
        sec = (int) Math.ceil(time3);
        return sec;
    }

    public static LocalDate date2LocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalTime date2LocalTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalTime();
    }

    public static LocalDateTime date2LocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static String localDate2Str(LocalDate localDate, String formatStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatStr);
        return formatter.format(localDate);
    }

    public static String localTime2Str(LocalTime localTime, String formatStr) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatStr);
        return formatter.format(localTime);
    }

    public static LocalTime str2LocalTime(String str, String formatStr) {
        DateTimeFormatter localTimeFormat = DateTimeFormatter.ofPattern(formatStr);
        return LocalTime.parse(str,localTimeFormat);
    }

    public static int getMinutesBetweenStEt(LocalDateTime st, LocalDateTime et) {
        long times = Duration.between(st, et).toMillis();
        long mins = times / (60*1000);
        if (times % (1000*60) > 0) {
            mins = (mins + 1);
        }
        return (int)mins;
    }

    public static int getDaysBetweenStEt(LocalDateTime st, LocalDateTime et) {
        long times = Duration.between(st, et).toHours();
        long days = times / 24;
        if (times % 24 > 0) {
            days = (days + 1);
        }
        return (int)days;
    }

    public static boolean isBeforeOrEqual(LocalDateTime time1, LocalDateTime time2) {
        return time1.isBefore(time2) || time1.isEqual(time2);
    }

    /**
     * 获取两个日期之间的所有日期
     */
    public static List<String> getDays(String startTime, String endTime, String formatStr) throws ParseException {
        // 返回的日期集合
        List<String> days = new ArrayList<>();
        DateFormat dateFormat = new SimpleDateFormat(formatStr);
        Date start = dateFormat.parse(startTime);
        Date end = dateFormat.parse(endTime);
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(start);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
        while (tempStart.before(tempEnd)) {
            days.add(dateFormat.format(tempStart.getTime()));
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        return days;
    }

    public static int getDateDetail(Date date, int detail) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(detail);
    }

    /**
     * 获取想要的年份
     * @param year
     * @param i
     * @return
     */
    public static String getYearBySet(String year,int i) throws ParseException {
        Date sYear = getDateByString(year,"yyyy");
        Calendar temp = Calendar.getInstance();
        temp.setTime(sYear);
        temp.set(Calendar.DAY_OF_YEAR,i);
        return toString(temp.getTime(),"yyyy");
    }

    public static int getDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static Date toNextMonthDayStartPoint(Date d) {
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, 1);
        return c.getTime();
    }

    /**
     * 转换时间戳
     * @param time
     * @param format
     * @return
     */
    public static String toTimeStamp(Long time, String format) {
        return new SimpleDateFormat(format).format(time);
    }

    public static Date parseTimeStamp2Date(Long time) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(time);
        return c.getTime();
    }

    /**
     * time1 > time2 则 =1，time1 < time1 则 =-1，
     * @param time1
     * @param time2
     * @return
     */
    public static int compare(LocalDateTime time1,LocalDateTime time2){
        if(time1.isBefore(time1)) {
            return -1;
        } else if(time1.isAfter(time2)) {
            return 1;
        }
        return 0;
    }

    /**
     * 时间戳转换 天 小时 分 秒
     * @param time 秒
     */
    public static StringBuffer timeFormat(long time){
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        StringBuffer sBuffer = new StringBuffer();
        if(time >= 24 * 60 * 60){
            day = time / (24 * 60 * 60);
            hour = time / (60 * 60) - day * 24;
            if (day > 0) {
                sBuffer.append(day+"天");
            }
        }else{
            hour = time / (60 * 60);
        }
        min = time - (hour * 60 * 60) - (day * 24 * 60 * 60);
        sBuffer.append(hour+"小时");
        min = min / (60);
        sBuffer.append(min+"分");
        sec = time - (hour * 60 * 60) - (day * 24 * 60 * 60) - (min * 60);
        if (sec > 0) {
            sBuffer.append(sec+"秒");
        }
        return sBuffer;
    }

    /**
     * 判断当前日期时间是否为白天
     * @param dayStart 开始时间
     * @param dayEnd 结束时间
     * @param checkTime 检查的时间点
     * @return true 是 false 否
     * @throws ParseException
     */
    public static boolean isInDay(String dayStart, String dayEnd, Date checkTime) throws ParseException {
        String today = TimeUtil.getNowString("yyyy-MM-dd");
        Date startTime = TimeUtil.getDateByString(today + " " + dayStart, TimeUtil.formatWithHM);
        Date endTime = TimeUtil.getDateByString(today + " " + dayEnd, TimeUtil.formatWithHM);
        if (checkTime.after(startTime) && checkTime.before(endTime)) { //白天
            return true;
        }
        return false;
    }

}
